package com.jiangnan.config;

import com.jiangnan.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author chenliang
 * @email wschenliang@aliyun.com
 */
public class AddressMap {
    private final static Logger logger= LoggerFactory.getLogger(AddressMap.class);
    protected Properties address = new Properties();
    private static final String PATH = AddressMap.class.getResource("/").getPath()+"address.properties";
    private static AddressMap addressMap;

    //单例模式
    private AddressMap() {

        try {
            address.load(new FileInputStream(PATH));
        } catch (IOException exception) {
            logger.error(exception.getMessage());
        }

    }

    public static AddressMap newInstance() {
        if (addressMap == null) {
            addressMap = new AddressMap();
        }
        return addressMap;
    }

    public  String getAddressByMac(String macAddr) {
        if (StringUtils.isBlank(macAddr)) {
            return null;
        }
        macAddr = macAddr.toUpperCase().replace(".", "-").substring(0,8);
        return address.getProperty(macAddr);
    }

    public static void main(String[] args) {
        String addressByMac = AddressMap.newInstance().getAddressByMac("50-DA-00");
        System.out.println(addressByMac);
    }

}
